//% color="#66CDAA" iconWidth=50 iconHeight=40
namespace DFRobot_I2C_GNSS {
  //% block="DFRobot_I2C_GNSS Init " blockType="command" 

  export function DFRobot_I2C_GNSSInit(parameter: any, block: any) {

    Generator.addInclude("DFRobot_I2C_GNSSInit", "#include <DFRobot_GNSSAndRTC.h>");

    Generator.addObject("DFRobot_I2C_GNSSInit", "DFRobot_GNSSAndRTC_I2C", "gnss_pt(&Wire, MODULE_I2C_ADDRESS);");

    Generator.addSetup("DFRobot_I2C_GNSSInitsetup", `while(!gnss_pt.begin()){
    Serial.println("NO Deivces !");
    delay(1000);
  }
  gnss_pt.enable32k();
  gnss_pt.enablePower();
  gnss_pt.setGnss(gnss_pt.eGPS_BeiDou_GLONASS);`);

  }
  //% block="enablePower[SET]" blockType="command"
  //% SET.shadow="dropdown"   SET.options="SET"
  export function DFRobot_I2C_GNSSablePower(parameter: any, block: any) {
    let set = parameter.SET.code;
    Generator.addCode([`gnss_pt.${set}ablePower()`, Generator.ORDER_UNARY_POSTFIX])

  }
  //% block="set time [Y][M][D][H][MM][S] " blockType="command"
  //% Y.shadow="number"   Y.defl="2024"
  //% M.shadow="number"   M.defl="1"
  //% D.shadow="number"   D.defl="1"
  //% H.shadow="number"   H.defl="0"
  //% MM.shadow="number"   MM.defl="0"
  //% S.shadow="number"   S.defl="0"
  export function DFRobot_I2C_GNSSSetTime(parameter: any, block: any) {
    let Y = parameter.Y.code;
    let M = parameter.M.code;
    let D = parameter.D.code;
    let H = parameter.H.code;
    let MM = parameter.MM.code;
    let S = parameter.S.code;
    Generator.addCode(`gnss_pt.setHourSystem(gnss_pt.e24hours);//Set display format
  gnss_pt.setTime(${Y},${M},${D},${H},${MM},${S});`)

  }

  //% block="calibRTC time  " blockType="command"
  export function DFRobot_I2C_GNSScalibRTC(parameter: any, block: any) {
    Generator.addCode([`gnss_pt.calibRTC()`, Generator.ORDER_UNARY_POSTFIX])

  }
  //% block="calibRTC time [SR] " blockType="command"
  //% SR.shadow="range"   SR.params.min=1    SR.params.max=255    SR.defl=1
  export function DFRobot_I2C_GNSScalibRTC1(parameter: any, block: any) {
    let SR = parameter.SR.code;
    Generator.addCode([`gnss_pt.calibRTC(${SR})`, Generator.ORDER_UNARY_POSTFIX])


  }
  //% block="calibStatus ? " blockType="boolean"
  export function DFRobot_I2C_GNSScalibStatus(parameter: any, block: any) {
    Generator.addCode([`gnss_pt.calibStatus()==1`, Generator.ORDER_UNARY_POSTFIX])

  }
  //% block="Read the data once and save it and get the result" blockType="command"
  export function DFRobot_I2C_GNSSreaddata(parameter: any, block: any) {

    Generator.addCode(`DFRobot_GNSSAndRTC::sTim_t utc = gnss_pt.getUTC();
  DFRobot_GNSSAndRTC::sTim_t date = gnss_pt.getDate();
  DFRobot_GNSSAndRTC::sLonLat_t lat = gnss_pt.getLat();
  DFRobot_GNSSAndRTC::sLonLat_t lon = gnss_pt.getLon();
  DFRobot_GNSSAndRTC::sTimeData_t rtcdate = gnss_pt.getRTCTime();
  double high = gnss_pt.getAlt();
  uint8_t starUserd = gnss_pt.getNumSatUsed();
  double sog = gnss_pt.getSog();
  double cog = gnss_pt.getCog();
  String ymd = String(rtcdate.year)+String('/')+String(rtcdate.month)+String('/')+String(rtcdate.day);
  String hms = String(rtcdate.hour)+String(':')+String(rtcdate.minute)+String(':')+String(rtcdate.second);`)
  }


  //% block="set time [RTCDATE] " blockType="reporter"
  //% RTCDATE.shadow="dropdown"   RTCDATE.options="RTCDATE"
  export function DFRobot_I2C_GNSSGetTime(parameter: any, block: any) {
    let rtcdate = parameter.RTCDATE.code;

    Generator.addCode([`${rtcdate}`, Generator.ORDER_UNARY_POSTFIX])

  }


  //% block="Fetch [DATA] from result" blockType="reporter"
  //% DATA.shadow="dropdown"   DATA.options="DATA"
  export function DFRobot_I2C_GNSSgetData(parameter: any, block: any) {
    let data = parameter.DATA.code;
    Generator.addCode([`${data}`, Generator.ORDER_UNARY_POSTFIX]);

  }



}